package com.ruoyi.device.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.device.domain.TmdDeviceInformation;
import com.ruoyi.device.service.ITmdDeviceInformationService;
import com.ruoyi.device.service.ITmdDeviceRecordService;
import com.ruoyi.device.vo.AllDeviceInfoVo;
import com.ruoyi.meeting.domain.AjaxTableResult;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.utils.DateUtils.getNowDate;

/**
 * 仪器信息Controller
 * 
 * @author ruoyi
 * @date 2024-04-26
 */
@Controller
@RequestMapping("/device/information")
public class TmdDeviceInformationController extends BaseController
{
    private String prefix = "device/information";

    @Autowired
    private ITmdDeviceInformationService tmdDeviceInformationService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ITmdDeviceRecordService tmdDeviceRecordService;

    @RequiresPermissions("device:information:view")
    @GetMapping()
    public String information()
    {
        return prefix + "/information";
    }

    /**
     * 查询仪器信息列表
     */
    @RequiresPermissions("device:information:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TmdDeviceInformation tmdDeviceInformation)
    {
        startPage();
        List<TmdDeviceInformation> list = tmdDeviceInformationService.selectTmdDeviceInformationList(tmdDeviceInformation);
        return getDataTable(list);
    }

    /**
     * 导出仪器信息列表
     */
    @RequiresPermissions("device:information:export")
    @Log(title = "仪器信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TmdDeviceInformation tmdDeviceInformation)
    {
        List<TmdDeviceInformation> list = tmdDeviceInformationService.selectTmdDeviceInformationList(tmdDeviceInformation);
        ExcelUtil<TmdDeviceInformation> util = new ExcelUtil<TmdDeviceInformation>(TmdDeviceInformation.class);
        return util.exportExcel(list, "仪器信息数据");
    }

    /**
     * 新增仪器信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存仪器信息
     */
    @RequiresPermissions("device:information:add")
    @Log(title = "仪器信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TmdDeviceInformation tmdDeviceInformation)
    {
        return toAjax(tmdDeviceInformationService.insertTmdDeviceInformation(tmdDeviceInformation));
    }

    /**
     * 修改仪器信息
     */
    @RequiresPermissions("device:information:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TmdDeviceInformation tmdDeviceInformation = tmdDeviceInformationService.selectTmdDeviceInformationById(id);
        mmap.put("tmdDeviceInformation", tmdDeviceInformation);
        return prefix + "/edit";
    }

    @RequiresPermissions("device:information:edit")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        TmdDeviceInformation tmdDeviceInformation = tmdDeviceInformationService.selectTmdDeviceInformationById(id);
        mmap.put("tmdDeviceInformation", tmdDeviceInformation);
        return prefix + "/detail";
    }

    /**
     * 修改保存仪器信息
     */
    @RequiresPermissions("device:information:edit")
    @Log(title = "仪器信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdDeviceInformation tmdDeviceInformation)
    {
        return toAjax(tmdDeviceInformationService.updateTmdDeviceInformation(tmdDeviceInformation));
    }

    /**
     * 删除仪器信息
     */
    @RequiresPermissions("device:information:remove")
    @Log(title = "仪器信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tmdDeviceInformationService.deleteTmdDeviceInformationByIds(ids));
    }


    @GetMapping("/getDeptList")
    @ResponseBody
    public AjaxTableResult<SysDept> getDeptList() {
        List<SysDept> userList = deptService.selectAllDeptList();
        AjaxTableResult<SysDept> ajaxTableResult = new AjaxTableResult();
        ajaxTableResult.setRows(userList);
        ajaxTableResult.setTotal(userList.size());
        return ajaxTableResult;
    }

    /**
     *  出库
     *  出库之后记录出库的人 出库的时间
     * */
    @RequestMapping("/dfs")
    @ResponseBody
    public AjaxResult dfs(@RequestParam("id") Long id) {
        try {

            //出库
            tmdDeviceInformationService.updateStatus(id);
            //通过id查询对象  此次更新状态的这个id对象
            AllDeviceInfoVo allDeviceInfoVo = tmdDeviceInformationService.selectMyUseById(id);
            //新建一个vo类 vo类的内容是 上面查出来的
            //自己设置登录人 和 操作的类型 和操作的时间
            // 获取当前的用户信息
            SysUser currentUser = ShiroUtils.getSysUser();
            //设置操作人id
            allDeviceInfoVo.setOperatePerson(currentUser.getUserId());
            //设置操作时间
            allDeviceInfoVo.setOperateTime(getNowDate());
            //设置操作类型
            allDeviceInfoVo.setOperateName(1);
            //设置仪器id
            allDeviceInfoVo.setDeviceId(id);
            allDeviceInfoVo.setGooutFlag(0);
            //插入到sql中
            tmdDeviceRecordService.insertUseRecord(allDeviceInfoVo);
            return AjaxResult.success("仪器出库成功");
        }catch (Exception e) {
            return AjaxResult.error("仪器出库失败" + e.getMessage());
        }
    }
}
